/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.


See LICENSE and AUTHORS for more information.
*/


#include <QStringList>
#include "buddyitem.h"

/**
* @brief Construct new BuddyItem
* @param data Vector of information to populate the item with.
*/
BuddyItem::BuddyItem(const QVector<QVariant> &data, BuddyItem *parent)
{
    parentItem = parent;
    itemData = data;
}

/**
* @brief desctructor
*/
BuddyItem::~BuddyItem()
{
    qDeleteAll(childItems);
}

/**
*  @brief get a child item
*  @return the child with index #number
*  @param number the index of the child to be returned
*/
BuddyItem *BuddyItem::child(int number)
{
    return childItems.value(number);
}

/**
* @brief get the count of childs for this item
* @return the count of childs
*/
int BuddyItem::childCount() const
{
    return childItems.count();
}

/**
* @brief get this items child number relative to its parent
* @return the index of this item within his parent or 0
*/
int BuddyItem::childNumber() const
{
    if (parentItem)
        return parentItem->childItems.indexOf(const_cast<BuddyItem*>(this));

    return 0;
}

/**
* @return the count of columns in this item's data
*/
int BuddyItem::columnCount() const
{
    return itemData.count();
}

/**
* @brief get the {#column}th column from this item's data
* @return the QVariant found in the requested column of data
*/
QVariant BuddyItem::data(int column) const
{
    return itemData.value(column);
}

/**
* @brief add Children to this item
*
* Inserts #count children with #columns columns
* of data right after the (#position)nd child.
*
* @param position the child index after which the children should be inserted
* @param count the count of to children to insert
* @param columns the count of columns to insert for each child
* @return false for an invalid value of #position, true otherwise
*/
bool BuddyItem::insertChildren(int position, int count, int columns)
{
    if (position < 0 || position > childItems.size())
        return false;

    for (int row = 0; row < count; ++row) {
        QVector<QVariant> data(columns);
        BuddyItem *item = new BuddyItem(data, this);
        childItems.insert(position, item);
    }

    return true;
}

/**
* @brief insert columns of data into the item
*
* Inserts #columns columns into the data right after
* the {#position}th column AND recursivly does the
* same for each child
*
* @param position the column after which to insert new columns
* @param columns the count of columns to insert
* @return false for invalid values of #position, true otherwise
*/
bool BuddyItem::insertColumns(int position, int columns)
{
    if (position < 0 || position > itemData.size())
        return false;

    for (int column = 0; column < columns; ++column)
        itemData.insert(position, QVariant());

    foreach (BuddyItem *child, childItems)
        child->insertColumns(position, columns);

    return true;
}


/**
* @brief get this item's parent
* @return the parent of this item
*/
BuddyItem *BuddyItem::parent()
{
    return parentItem;
}

/**
* @brief removes #count children, starting with the child at position #position.
*
* @param position the first child to remove
* @param count how many children to remove
* @return false if params indicate children that don't exist, true otherwise
*/
bool BuddyItem::removeChildren(int position, int count)
{
    if (position < 0 || position + count > childItems.size())
        return false;

    for (int row = 0; row < count; ++row)
        delete childItems.takeAt(position);

    return true;
}

/**
* @brief remove Columns from data
*
* removes #columns columns from data, starting at column #position
* also recursivly does the same for all children
*
* @param position the first column to delete
* @param columns how many columns to delete?
* @return false if the params indicate columns that dont exist, true otherwise
*/
bool BuddyItem::removeColumns(int position, int columns)
{
    if (position < 0 || position + columns > itemData.size())
        return false;

    for (int column = 0; column < columns; ++column)
        itemData.remove(position);

    foreach (BuddyItem *child, childItems)
        child->removeColumns(position, columns);

    return true;
}

/**
* @brief sets the (#column)th column's data to #value
*
* @param column the index of the column to update
* @param value the data to set this column to
* @return false if #column is no valid index in data, true otherwise
*/
bool BuddyItem::setData(int column, const QVariant &value)
{
    if (column < 0 || column >= itemData.size())
        return false;

    itemData[column] = value;
    return true;
}

